package com.xu.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xu.common.enums.StationNumTypeEnum;
import com.xu.modules.entity.StationEntity;
import com.xu.modules.mapper.StationMapper;
import com.xu.modules.service.StationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 站点信息 服务实现类
 * </p>
 *
 * @author xu
 * @since 2022-07-27
 */
@Service
@RequiredArgsConstructor
public class StationServiceImpl extends ServiceImpl<StationMapper, StationEntity> implements StationService {


    @Override
    public void changeStationNum(StationNumTypeEnum stationNumTypeEnum) {
        LambdaQueryWrapper<StationEntity> lqw = Wrappers.lambdaQuery();
        lqw.last("limit 1");
        StationEntity stationEntity = baseMapper.selectOne(lqw);
        stationEntity = stationEntity == null ? new StationEntity() : stationEntity;
        if(stationNumTypeEnum.equals(StationNumTypeEnum.VISITOR)){
            int num = (stationEntity.getVisitorNum() == null ? 0 : stationEntity.getVisitorNum()) + 1;
            stationEntity.setVisitorNum(num);
        }
        if(stationNumTypeEnum.equals(StationNumTypeEnum.FRIEND)){
            int num = (stationEntity.getFriendNum() == null ? 0 : stationEntity.getFriendNum()) + 1;
            stationEntity.setFriendNum(num);
        }
        if(stationNumTypeEnum.equals(StationNumTypeEnum.ARTICLE)){
            int num = (stationEntity.getArticleNum() == null ? 0 : stationEntity.getArticleNum()) + 1;
            stationEntity.setArticleNum(num);
        }
        if(stationNumTypeEnum.equals(StationNumTypeEnum.REGISTER)){
            int num = (stationEntity.getRegisterNum() == null ? 0 : stationEntity.getRegisterNum()) + 1;
            stationEntity.setRegisterNum(num);
        }
        if(stationNumTypeEnum.equals(StationNumTypeEnum.COMMENT)){
            int num = (stationEntity.getCommentNum() == null ? 0 : stationEntity.getCommentNum()) + 1;
            stationEntity.setCommentNum(num);
        }
        updateById(stationEntity);
    }

    @Override
    public StationEntity selectStation() {
        LambdaQueryWrapper<StationEntity> lqw = Wrappers.lambdaQuery();
        lqw.last("limit 1");
        StationEntity stationEntity = baseMapper.selectOne(lqw);
        return stationEntity == null ? new StationEntity() : stationEntity;
    }
}
